/*
 * Copyright © 2021 http://www.TestCopyright.com/ All rights reserved.
 */
package com.xjh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.bo.StudentBO;
import com.xjh.service.StudentService;
import com.xjh.utils.resultutils.ResponseObj;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： 徐建华
 * @date： 2021/11/28
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {
    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 查询所有学生带分页
     *
     * @param params params
     * @return com.xjh.utils.resultutils.ResponseObj
     * @method queryAll
     * @author XuJianHua
     * @date 2022/7/18 11:01
     * @description
     */
    @PostMapping("/queryAll")
    public ResponseObj queryAll(@RequestBody Map<String, Object> params) {
        Integer start = (Integer) params.get("start");
        Integer size = (Integer) params.get("size");
        Page<StudentBO> pageParam = new Page<>(start, size);

        IPage<StudentBO> pageModel =
                studentService.queryAll(pageParam);
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(pageModel);
    }


    /**
     * 查询所有同学总成绩,名字并按分数排名
     *
     * @return com.xjh.utils.resultutils.ResponseObj
     * @method queryScoreSort
     * @author XuJianHua
     * @date 2022/7/18 11:04
     * @description
     */
    @GetMapping("/queryScoreSort")
    public ResponseObj queryScoreSort() {
        List<Map<String, Object>> queryScoreSort = studentService.queryScoreSort();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(queryScoreSort);
    }


    /**
     * 一对一查询：根据查询学生的所有账户（account表），同时得到该学生的信息(student表)，一个账户，对应一个学生
     *
     * @return com.xjh.utils.resultutils.ResponseObj
     * @method queryAccountAndStudent
     * @author XuJianHua
     * @date 2022/7/18 11:04
     * @description
     */
    //select * from account a LEFT JOIN  student s ON a.stu_id = s.id;
    @GetMapping("/queryAccountAndStudent")
    public ResponseObj queryAccountAndStudent() {
        List<Map<String, Object>> queryAccountAndStudent = studentService.queryAccountAndStudent();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(queryAccountAndStudent);
    }

    /**
     * 一对多查询（左外连接查询）：查询所有学生的所有账户，一个学生可能有多个账户，没有账户则返回null
     *
     * @return com.xjh.utils.resultutils.ResponseObj
     * @method allAccountOfStudent
     * @author XuJianHua
     * @date 2022/7/18 11:04
     * @description
     */
    //SELECT
    //	*
    //FROM
    //	student s
    //	LEFT OUTER JOIN account a ON a.stu_id = s.id
    @GetMapping("/allAccountOfStudent")
    public ResponseObj allAccountOfStudent() {
        List<Map<String, Object>> queryScoreSort = studentService.allAccountOfStudent();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(queryScoreSort);
    }
    /**
     * @Author: XuJianHua
     * @Date 2022/10/18 16:44
     * @Description: 测试接口命名相同，参数不同，是否会自动匹配到对应方法
     **/
    @GetMapping("/22")
    public ResponseObj test1(@RequestParam String sex) {

        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok("匹配到了根据性别查询接口"+ sex);
    }
    @GetMapping
    public ResponseObj test2(@RequestParam String className) {
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok("匹配到了根据性别查询接口"+ className);
    }
}
